<template>
	<view class="tab">
		<scroll-view class="tab-scroll" scroll-x="true">
			<view class="tab-scroll_box">
				<view v-for="(item,index) in list" :key="index" class="tab-scroll_item"
					:class="{active: activeIndex === index}" @click="clickTab(item,index)">{{item.name}}</view>
			</view>
		</scroll-view>
		<view class="tab-icons">
			<uni-icons type="gear" size="26" color="#666"></uni-icons>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			// 接受外面传进来的数据
			list: {
				// 接收的是一个数组
				type: Array,
				default () {
					return []
				}
			}
		},
		name: "tab",
		data() {
			return {
				activeIndex: 0
			};
		},
		methods: {
			clickTab(item, index) {
				// console.log(item, index)
				this.activeIndex = index
				// 传出一个自定义事件tab
				this.$emit('tab',{
					data:item,
					index:index
				})
			}
		}
	}
</script>

<style lang="scss">
	.tab {
		display: flex;
		width: 100%;
		border-bottom: 1px solid #f5f5f5;
		background-color: #fff;
		box-sizing: border-box;
	}

	.tab-scroll {
		// 让元素撑满整个屏幕
		flex: 1;
		overflow: hidden;
		box-sizing: border-box;

		.tab-scroll_box {
			display: flex;
			// 垂直居中
			align-items: center;
			// 不让它换行
			flex-wrap: nowrap;
			height: 45px;
			box-sizing: border-box;

			.tab-scroll_item {
				// 不让挤压
				flex-shrink: 0;
				padding: 0 10px;
				color: #333;
				font-size: 14px;

				&.active {
					color: $mk-base-color !important;
				}
			}
		}
	}

	.tab-icons {
		position: relative;
		display: flex;
		// 垂直方向居中
		justify-content: center;
		// 水平方向居中
		align-items: center;
		width: 45px;

		&::after {
			// 伪类中必须携带 content
			content: '';
			position: absolute;
			top: 12px;
			bottom: 12px;
			left: 0;
			width: 1px;
			background-color: #ddd;
		}
	}
</style>
